package com.liuxl.core.controller.gen;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.liuxl.core.common.base.BaseController;
import com.liuxl.core.common.domain.AjaxResult;
import com.liuxl.core.common.log.Log;
import com.liuxl.core.model.auto.OrganDic;
import com.liuxl.core.model.auto.TsysUser;
import com.liuxl.core.model.custom.TableSplitResult;
import com.liuxl.core.model.custom.Tablepar;
import com.liuxl.core.model.custom.TitleVo;
import com.liuxl.core.service.OrganDicService;

import io.swagger.annotations.Api;

@Controller
@RequestMapping("OrganDicController")
@Api(value = "机构管理")
public class OrganDicController extends BaseController{
	
	private String prefix = "gen/organDic";
	@Autowired
	private OrganDicService organDicService;
	
	@GetMapping("view")
	@RequiresPermissions("gen:organDic:view")
    public String view(ModelMap model)
    {	
		String str="机构管理";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }
	
	//@Log(title = "机构管理集合查询", action = "111")
	@PostMapping("list")
	@RequiresPermissions("gen:organDic:list")
	@ResponseBody
	public Object list(Tablepar tablepar,String searchTxt){
		PageInfo<OrganDic> page=organDicService.list(tablepar,searchTxt) ; 
		TableSplitResult<OrganDic> result=new TableSplitResult<OrganDic>(page.getPageNum(), page.getTotal(), page.getList()); 
		return  result;
	}
	
	/**
	 * 权限列表
	 * @param tablepar
	 * @param searchTxt 搜索字符
	 * @return
	 */
	@PostMapping("list2")
	@ResponseBody
	public Object list2(Tablepar tablepar,String searchTxt){
		List<OrganDic> page= organDicService.list2(searchTxt) ;
		return  page;
	}
	
	/**
     * 	跳转到菜单树页面
     * @return
     */
    @GetMapping("tree")
    public String Tree(){
    	 return prefix + "/tree";
    }
	
    /**
     * 获取菜单树
     * @param pid 父id【没用到】
     * @return
     */
    @PostMapping("tree/{pid}")
    @ResponseBody
    public AjaxResult Tree(@PathVariable("pid") String pid){
    	return retobject(200, organDicService.getbooBootstrapTreePerm(null));
    }
    
    
    
	/**
     * 新增
     */

    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
    	//添加用户列表
		List<TsysUser> tsysUserList=sysUserService.queryList();
		modelMap.put("tsysUserList",tsysUserList);
        return prefix + "/add";
    }
	
	@Log(title = "机构管理新增", action = "111")
	@PostMapping("add")
	@RequiresPermissions("gen:organDic:add")
	@ResponseBody
	public AjaxResult add(OrganDic organDic){
		int b=organDicService.insertSelective(organDic);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 删除用户
	 * @param ids
	 * @return
	 */
	@Log(title = "机构管理删除", action = "111")
	@PostMapping("remove")
	@RequiresPermissions("gen:organDic:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=organDicService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 检查用户
	 * @param tsysUser
	 * @return
	 */
	@PostMapping("checkNameUnique")
	@ResponseBody
	public int checkNameUnique(OrganDic organDic){
		int b=organDicService.checkNameUnique(organDic);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}
	
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
		OrganDic od = organDicService.selectByPrimaryKey(id);
		OrganDic pod = organDicService.selectByPrimaryKey(od.getPid());
		if(pod==null) {
			pod = new OrganDic();
		}
        mmap.put("OrganDic", od);
        mmap.put("ParOrganDic", pod);
        
        //添加用户列表
  		List<TsysUser> tsysUserList=sysUserService.queryList();
  		mmap.put("tsysUserList",tsysUserList);
  		
        return prefix + "/edit";
    }
	
	/**
     * 	修改保存
     */
    @Log(title = "机构管理修改", action = "111")
    @RequiresPermissions("gen:organDic:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrganDic record)
    {
        return toAjax(organDicService.updateByPrimaryKeySelective(record));
    }

    
    

	
}
